<?php

namespace App\Admin\Controllers;

use App\Models\Student;
use App\Models\Crousesheet;
use App\Models\Notice;
use App\Models\Zuoye_record;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Zuoye_recordController  extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.zuoye_record'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());  
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('admin.zuoye_record'));
            $content->description(trans('admin.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.zuoye_record'));
            $content->description(trans('admin.create'));
            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Zuoye_record::class, function (Grid $grid) {
            $notice_id = request('notice_id');
            $grid->model()->where('notice_id',$notice_id);
            $grid->id('序号')->sortable();
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            });
            
            $grid->zuoye_status(trans('admin.zuoye_status'))->display(function($zuoye_status){
                if ($this->zuoye_status == 1) {
                    return "准交";
                }elseif ($this->zuoye_status == 2) {
                    return "未交";
                }elseif ($this->zuoye_status == 3) {
                    return "补交";
                }else{
                    return 'N/A';
                }
            })->sortable();
            
            $grid->zuoye_score(trans('admin.zuoye_score'));
            $grid->zuoye_desc(trans('admin.zuoye_desc'));
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
            $grid->disableCreateButton();
            //$grid->disableActions();
            //禁用导出按钮
            //$grid->disableExport();
            $filename="作业记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            //$grid->disableFilter();//禁用查询
        });
    }

    protected function form()
    { 
        $form = new Form(new Zuoye_record());
        $form->display('id', 'ID');
        $form->radio('zuoye_status')->options([
            1 => '准交',
            2 => '未交',
            3 => '补交',
        ]);
        //$form->select('zuoye_status', trans('admin.zuoye_status'))->options(['1'=>'准交','2'=>'未交'])->rules('required')->addElementLabelClass('must-input');
        $form->number('zuoye_score',trans('admin.zuoye_score'));
        $form->textarea('zuoye_desc',trans('admin.zuoye_desc'));
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        return $form;
    }
}
